<?php

namespace co2_OS;

function parse_manifest($type,$name,$version,$channel=null)

{
		$manifestf=sysvar::get($type=='app'?'apps':$type)."/$name/";
		
		if($type=='app') $manifestf.="$channel/";
		
		$manifestf.=$version;
		
		$manifestf.='/manifest.';
		
		$manifestf.=$type=='app'?'xml':'co2';

		if(!file_exists($manifestf))
		throw new \Exception('Manifest file not found for '."$type $name version $version looked in $manifestf");
		
		$manifest=new \DOMDocument;
		
		try
		{
			$manifest->load($manifestf);
		}
		catch(\Exception $e)
		{
			throw new \Exception('Could not DOMified manifest for '."$type $name version $version looked in $manifestf");
		}
	
		$man=new \stdClass;
	
		$man->dependencies=array();	
	
		foreach($manifest->getElementsByTagName('dependency') as $dependency)
	
		{
			$d=new \stdClass;
		
			$d->type=$dependency->getAttribute('type');
			
			switch($d->type)
			
			{
			  case'api':
			  
			    $d->version=$dependency->getAttribute('version');
			    
			    $d->channel=$dependency->getAttribute('channel');
			  
			  break;
			  
			  case'app';
			  
			    $d->name=$dependency->getAttribute('name');
			    
			    $d->version=$dependency->getAttribute('version');
			    
			    $d->channel=$dependency->getAttribute('channel');
			  
			  break;
			  
			  case'lib';
			  
			    $d->name=$dependency->getAttribute('name');
			    
			    $d->version=$dependency->getAttribute('version');
			  
			  break;
			}
		
			$man->dependencies[]=$d;
		}
	
		return $man;
}
